package com.xykj.enterprise.wechat.busi.api.identity;

import com.xykj.enterprise.wechat.bean.busi.vo.GetAgentConfigVo;
import com.xykj.enterprise.wechat.busi.config.ExtClient;
import com.xykj.enterprise.wechat.busi.service.corp.CorpService;
import com.xykj.enterprise.wechat.busi.service.identity.JsapiTicketService;
import com.xykj.enterprise.wechat.busi.service.identity.JsapiTicketUtil;
import com.xykj.enterprise.wechat.util.other.UuidUtil;
import com.xykj.enterprise.wechat.util.wechat.JSAPIUtil;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Calendar;

/**
 * @Author zhouxu
 * @create 2021-04-02 15:56
 */
@Function(description = "客户端 wx.agentConfig 接口", parameters = {
        @Parameter(name = "corpid", type = Type.String, description = "授权方corpid", required = true),
        @Parameter(name = "agent_id", type = Type.String, description = "agent_id", required = true),
        @Parameter(name = "suite_id", type = Type.String, description = "suite_id", required = true),
        @Parameter(name = "secret", type = Type.String, description = "secret", required = true),

        @Parameter(name = "url", type = Type.String, description = "回调地址", required = true),

})
@Component
@Slf4j
public class GetAgentConfig implements Action {

    @Autowired
    private CorpService corpService;
    @Autowired
    private JsapiTicketService jsapiTicketService;
    @Autowired
    private ExtClient extClient;

    @Override
    public Response execute(Request request) throws Exception {
        String corpid = request.getString("corpid");
        String agent_id = request.getString("agent_id");
        String suite_id = request.getString("suite_id");
        String secret = request.getString("secret");


        try {
            // 获取jsapi_ticket
            String jsapi_ticket = JsapiTicketUtil.getAgentTicket(
                    jsapiTicketService,
                    corpid,
                    agent_id,
                    suite_id,
                    secret,
                    corpService,
                    extClient
            );

            String noncestr = UuidUtil.getNoSpecialChar();
            long timestamp = Calendar.getInstance().getTimeInMillis();
            String url = request.getString("url");

            GetAgentConfigVo vo = new GetAgentConfigVo();
            vo.setCorpid(corpid);
            vo.setAgentid(agent_id);
            vo.setTimestamp(String.valueOf(timestamp));
            vo.setNonceStr(noncestr);
            vo.setSignature(JSAPIUtil.getSignature(jsapi_ticket, noncestr, timestamp, url));
            return Response.success().put("data", vo);

        } catch (Exception e) {
            log.error("", e);
            return Response.fail(e);
        }
    }
}
